<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

class Model_Departemen extends MY_Model {
	
	public function __construct() {

		parent::__construct();

		$this->table_name = 'aka_departemen';

		$this->primary_key = 'aka_departemen.departemen_id';

	}
	function get_departemen($aktif=null){
		$this->load->database();
			$filter = "";
			if($aktif != null)
				$filter = "WHERE {$this->table_name}.aktif='$aktif'";
			$sql = "SELECT aka_departemen.*,mst_pegawai.nama as name FROM aka_departemen JOIN mst_pegawai ON aka_departemen.kepsek_id=mst_pegawai.pegawai_id $filter ORDER BY aka_departemen.urutan ASC";
			$query = $this->db->query($sql);
		$this->db->close();
		return $query;
	}
	
	function get_departemen_by($id){
		$this->load->database();		
		$sql = "SELECT * FROM aka_departemen WHERE departemen_id='$id'";
		$query = $this->db->query($sql);
		$this->db->close();
		return $query;
	}
	
	function add($data){
		$this->load->database();
			$this->db->trans_start();
				$this->db->insert('aka_departemen',$data);
			$this->db->trans_complete();
			
			if ($this->db->trans_status() === FALSE)
			{
				$this->session->set_flashdata('error','Data gagal disimpan.');
			}else{
				$this->session->set_flashdata('success','Data berhasil disimpan.');
			}
		$this->db->close();
	}
	
	function edit($id,$data){
		$this->load->database();
			$this->db->trans_start();
				$this->db->where('departemen_id',$id);
				$this->db->update('aka_departemen',$data);
			$this->db->trans_complete();
			
			if ($this->db->trans_status() === FALSE)
			{
				$this->session->set_flashdata('error','Data gagal disimpan.');
			}else{
				$this->session->set_flashdata('success','Data berhasil disimpan.');
			}
		$this->db->close();
	}
	
	function delete($id){
		$this->load->database();
			$this->db->trans_start();
				echo $sql = "DELETE FROM aka_departemen WHERE departemen_id='$id'";
				$this->db->query($sql);
			$this->db->trans_complete();
			
			if ($this->db->trans_status() === FALSE)
			{
				$this->session->set_flashdata('error','Data gagal dihapus.');
			}else{
				$this->session->set_flashdata('success','Data berhasil dihapus.');
			}
		$this->db->close();
	}
	
}

?>